import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Pageable, PageableResponse, Sysrole, SysrolePermission, UserSysrole, UserSysrolePK } from '../domain/sparrow';
import { SparrowService } from './sparrow.service';

@Injectable({
  providedIn: 'root'
})
export class SysroleService {

  host: string = "http://localhost:8091/api";

  constructor(private httpClient: HttpClient) { }

  public list(pageable: Pageable|any) {
    pageable.page = pageable.page -1
    return this.httpClient.get<PageableResponse>(this.host + "/sysroles",{params: pageable});
  }

  public add(sysroles: Sysrole[]) {
    return this.httpClient.post<any>(this.host + "/sysroles/batch", sysroles);
  }

  public save(sysrole: Sysrole) {
    if (sysrole._links === undefined)
      return this.httpClient.post<Sysrole>(this.host + "/sysroles", sysrole);
    else
      return this.httpClient.patch<Sysrole>(sysrole._links.self.href, sysrole);

  }

  public del(ids: string[]) {
    return this.httpClient.delete<any>(this.host + "/sysroles/batch", { body: ids });
  }

  delete(sysrole: Sysrole) {
    return this.httpClient.delete<Sysrole>(sysrole._links.self.href);
  }

  public addPermission(userSysroles: UserSysrole[]) {
    return this.httpClient.post<any>(this.host + "/sysroles/userSysroles", userSysroles);
  }

  public delPermission(userSysrolePKs: UserSysrolePK[]) {
    return this.httpClient.delete<any>(this.host + "/sysroles/userSysroles", { body: userSysrolePKs });
  }

}
